package cn.nextProject.controller;

import cn.nextProject.entity.Result;
import cn.nextProject.entity.RoleGroup;
import cn.nextProject.entity.req.FindToPage;
import cn.nextProject.service.RoleGroupService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class RoleGroupController {

    @Autowired
    private RoleGroupService roleGroupService;

    @PostMapping("/findByRoleGroup")
    public Object findByRoleGroup(@RequestBody FindToPage<RoleGroup> findToPage){

        if (ObjectUtils.isEmpty(findToPage)){
            return new Result<>().fail("查询数据不能为空");
        }

        RoleGroup roleGroup = findToPage.getData();
        int pageNum = findToPage.getPageNum();
        int pageSize = findToPage.getPageSize();

        if (ObjectUtils.isEmpty(roleGroup)){
            return new Result<>().fail("搜索参数不能为空");
        }

        if (ObjectUtils.isEmpty(pageNum) || ObjectUtils.isEmpty(pageSize)){
            return new Result<>().fail("分页参数不能为空");
        }

        PageInfo<RoleGroup> byRoleGroup = roleGroupService.findByRoleGroup(roleGroup, pageNum, pageSize);
        return new Result<>().success("查询成功",byRoleGroup.getList(),byRoleGroup.getTotal());
    }

    @PostMapping("/saveRoleGroup")
    public Result<?> saveRoleGroup(@RequestBody RoleGroup roleGroup){

        if (ObjectUtils.isEmpty(roleGroup)){
            return new Result<>().fail("保存数据不能为空");
        }

        if (ObjectUtils.isEmpty(roleGroup.getRoleGroupName()) || "".equals(roleGroup.getRoleGroupName())){
            return new Result<>().fail("角色组名称不能为空");
        }

        roleGroupService.save(roleGroup);

        return new Result<>().success("保存成功",null);
    }

    @PostMapping("/updateRoleGroupState")
    public Result<?> updateRoleGroupState(@RequestBody RoleGroup roleGroup){

        if (ObjectUtils.isEmpty(roleGroup)){
            return new Result<>().fail("参数不能为空");
        }

        if (ObjectUtils.isEmpty(roleGroup.getRoleGroupId())){
            return new Result<>().fail("角色组ID不能为空");
        }
        if (ObjectUtils.isEmpty(roleGroup.getState())){
            return new Result<>().fail("角色组状态不能为空");
        }
        if (ObjectUtils.isEmpty(roleGroup.getRoleGroupName()) || "".equals(roleGroup.getRoleGroupName())){
            return new Result<>().fail("角色组名称不能为空");
        }

        RoleGroup roleGroupFind = roleGroupService.findOne(roleGroup.getRoleGroupId());
        if (ObjectUtils.isEmpty(roleGroupFind)){
            return new Result<>().fail("角色组不存在");
        }

        roleGroupService.update(roleGroup);//编辑

        return new Result<>().success("修改成功",null);
    }

    @GetMapping("/deleteRoleGroup")
    public Result<?> deleteRoleGroup(Long roleGroupId){

        if (ObjectUtils.isEmpty(roleGroupId)){
            return new Result<>().fail("角色组ID不能为空");
        }

        roleGroupService.delete(roleGroupId);
        return new Result<>().success("删除成功",null);
    }




}
